 <!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    

    <title>{$Think.config.site.title} - 库存管理</title>

    <meta name="keywords" content="{$Think.config.site.keywords}">

    <link href="{$Think.config.site.resource_url}css/bootstrap.min.css?v=3.3.5" rel="stylesheet">
    <link href="{$Think.config.site.resource_url}css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="{$Think.config.site.resource_url}css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="{$Think.config.site.resource_url}css/animate.min.css" rel="stylesheet">
    <link href="{$Think.config.site.resource_url}css/style.min.css?v=4.0.0" rel="stylesheet">

</head>
<style>
#listtable .fa{
	cursor:pointer;
	color:#1ab394;
	margin-right:1em;
}
th .tag{font-size: 12px; color: #e66c6c; font-weight: normal;}
.ibox-title .tag{font-size: 12px; color: #e66c6c; font-weight: normal; display: inline-block; margin-left: 20px;}
.specval-hide{display: none;}


.table>tbody>tr.active>td{background: rgb(26, 179, 148) !important}
.table>tbody>tr.finish>td{background: rgb(26, 179, 148) !important}
.error{background: #f00}
</style>
<body class="gray-bg">
	<form method="post" class="form-horizontal">
    <div class="wrapper wrapper-content animated fadeInRight">
      <div class="row">
            <div class="col-sm-4">
                <div>
                	<!-- <button type="button" class="btn btn-primary btn-sm" onclick="window.history.back()"><i class="fa fa-backward"></i> 返回</button> -->
                    <button type="button" class="btn btn-primary btn-sm" id="submit" ><i class="fa fa-sign-in"></i> 入库</button>
                </div>
            </div>
            <div class="col-sm-4">
                <div class="input-group">
                    <input type="text" name="scancode" id="scancode" class="input-sm form-control">
                    <span class="input-group-btn"> <a class="btn btn-sm btn-primary" id="entertype"> 切换到手动模式</a> </span>
                </div>
            </div>
      </div>
      
      <div class="row">
          <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>商品入库</h5>
                        <!-- <span class="tag">说明：</span> -->
                    </div>
                    <div class="ibox-content">
                        <div class="row">
                        	<div class="col-sm-6">
                        	</div>
                          	<div class="col-sm-6">
                                <div class="input-group pull-right">
               
                                </div>
                            </div>
                            
                        </div>
                        <div class="table-responsive">
                            <table class="table" id="listtable">
                                <thead>
                                    <tr>
                                        <!-- <th width="30"><input name="checkall" id="checkall"  type="checkbox" class="i-checks" value="1"></th> -->
                                        <th width="150">采购单_商品_规格ID</th>
                                        <th >商品图片</th>
                                        <th >商品名称</th>
                                        <th >应采数量</th>
                                        <th >实采数量</th>
                                        <th >已入库数量</th>
                                        <th >入库数量</th>
                                        <th >商城价格</th>
                                        <th >入库单价</th>
                                    </tr>
                                </thead>
                                <tbody>

                                	{volist name="list" id="vo" key="m"}
                                    <tr data-purchase_id="{$vo.purchase_id}" data-specval_id="{$vo.specval_id}">
                                        <!-- <td>
                                            <input name="ids[]"  type="checkbox" class="i-checks" value="{$vo.specval_id}">
                                        </td> -->
                                        <td>{$vo.purchase_id}_{$vo.goods_id}_{$vo.specval_id}</td>
                                        <td><img src="{$vo.picture}" width="30" height="30"></td>
                                        <td>{if condition="$vo.brand_name!=''"}（{$vo.brand_name}）{/if}{$vo.goods_name}【{$vo.specval_name}】</td>
                                        <td>{$vo.num}</td>
                                        <td><input type="text" name="real_num" class="form-control" placeholder="实采数量" value="{$vo.real_num}" autocomplete="off" disabled="disabled"></td>
                                        <td>{$vo.entstock_num}</td>
                                        <td><input type="text" name="entstock_num" class="form-control entstock_num" placeholder="本次入库数量" autocomplete="off"></td>
                                        <td>{$vo.mall_price}</td>
                                        <td><input type="text" name="enter_price" class="form-control" placeholder="本次入库单价" value="{$vo.cur_price}" autocomplete="off"></td>
                                    </tr>
                                   	{/volist}

                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>

        </div>
        
    </div>
    </form>
    <script src="{$Think.config.site.resource_url}js/jquery.min.js?v=2.1.4"></script>
    <script src="{$Think.config.site.resource_url}js/bootstrap.min.js?v=3.3.5"></script>
    <script src="{$Think.config.site.resource_url}js/plugins/peity/jquery.peity.min.js"></script>
    <script src="{$Think.config.site.resource_url}js/plugins/iCheck/icheck.min.js"></script>
    <script src="{$Think.config.site.resource_url}js/plugins/layer/layer.js"></script>
    <script src="{$Think.config.site.common_resource_url}js/majax.js"></script>
    <script src="{$Think.config.site.common_resource_url}js/jedit.js"></script>

    <script src="{$Think.config.site.resource_url}js/plugins/validate/jquery.validate.min.js"></script>
    <script src="{$Think.config.site.resource_url}js/plugins/validate/messages_zh.min.js"></script>
</body>

</html>
<script>

var layer_font='';

$('#submit').click(function(){
    $("form").submit();
})

function save(){
	var list=[];
    $('#listtable tbody tr').each(function(){
    	if ($(this).find('input[name="entstock_num"]').val()!='',$(this).find('input[name="entstock_num"]').val()>0) {
    		list.push({purchase_id:$(this).data('purchase_id'),specval_id:$(this).data('specval_id'),num:$(this).find('input[name="entstock_num"]').val(),real_num:$(this).find('input[name="real_num"]').val(),enter_price:$(this).find('input[name="enter_price"]').val()});
    	}
    })

    if (list.length>0) {
    	majax("{:url('entStock')}",{data:list},function(re){
    		if (re.code <= 0) {
    			layer.msg(re.msg,{icon:2});
    		}
    	});
    }
}

$("form").validate({
	debug:false,
	submitHandler:function(form){
		save();
	},    
	rules: {
		entstock_num: {
			digits:true,
			min:1
		},
	},
	messages: {
		entstock_num: {
			digits: '数量必须是整数',
			min:'数量不能小于1'
		},
	}
});

$('#scancode').keypress(function(e){
    var eCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
    if (eCode == 13){
        var code = $(this).val();
        $(this).val('');
        addnum(parseInt(code))
        
    }
})

function addnum(code) {
    var tr = $('tr[data-specval_id="'+code+'"]');
    if (tr.length<1) {
        // $('#scancode').val('');
        // layer.msg('商品不在该次入库单内');
        // $('.ibox-content').addClass('error');
        // setTimeout(function(){
        //     $('.ibox-content').removeClass('error');
        // },1000)
        // return false;
        // 
        // <tr data-purchase_id="{$vo.purchase_id}" data-specval_id="{$vo.specval_id}">
        //     <!-- <td>
        //         <input name="ids[]"  type="checkbox" class="i-checks" value="{$vo.specval_id}">
        //     </td> -->
        //     <td>{$vo.purchase_id}_{$vo.goods_id}_{$vo.specval_id}</td>
        //     <td><img src="{$vo.picture}" width="30" height="30"></td>
        //     <td>{if condition="$vo.brand_name!=''"}（{$vo.brand_name}）{/if}{$vo.goods_name}【{$vo.specval_name}】</td>
        //     <td>{$vo.num}</td>
        //     <td><input type="text" name="real_num" class="form-control" placeholder="实采数量" value="{$vo.real_num}" autocomplete="off" disabled="disabled"></td>
        //     <td>{$vo.entstock_num}</td>
        //     <td><input type="text" name="entstock_num" class="form-control entstock_num" placeholder="本次入库数量" autocomplete="off"></td>
        //     <td>{$vo.mall_price}</td>
        //     <td><input type="text" name="enter_price" class="form-control" placeholder="本次入库单价" value="{$vo.cur_price}" autocomplete="off"></td>
        // </tr>
        
        majax('{:url("scanadd",null,null,null)}',{specval_id:code},function(e){
            if (e.code>0) {
                var e = e.data;
                var html = '<tr data-specval_id="'+e.specval_id+'">\
                <td>'+e.goods_id+'_'+e.specval_id+'</td>\
                <td><img src="'+e.picture+'" width="30" height="30"></td>\
                <td>（'+e.brand_name+'）'+e.goods_name+'【'+e.specval_name+'】</td>\
                <td>'+e.num+'</td>\
                <td><input type="text" name="real_num" class="form-control" placeholder="实采数量" value="'+e.real_num+'" autocomplete="off" disabled="disabled"></td>\
                <td>'+e.entstock_num+'</td>\
                <td><input type="text" name="entstock_num" class="form-control entstock_num" placeholder="本次入库数量" value="1" autocomplete="off"></td>\
                <td>'+e.mall_price+'</td>\
                <td><input type="text" name="enter_price" class="form-control" placeholder="本次入库单价" value="'+e.cur_price+'" autocomplete="off"></td>\
                </tr>';
                $("#listtable tbody").prepend(html);
            }else{
                layer.msg(e.msg);
            }
        })
    }
    $(document).scrollTop(tr.offset().top-200);
    tr.addClass('active').siblings('tr').removeClass('active');
    setTimeout(function(){
        tr.removeClass('active');
    },1000)

    var num = 1
    if (tr.find('.entstock_num').val()) {
       num = parseInt(tr.find('.entstock_num').val())+num;
    }
    tr.find('.entstock_num').val(num);

    if (tr.find('.entstock_num').val() == tr.find('input[name="real_num"]').val()) {
        tr.addClass('finish');
    }

    // $("#listtable tbody").prepend(tr);
}

var entertype = 0; // 扫描模式 
var time = setInterval(function(){
    // $('#scancode').focus();
    scancodefocus();
},1000);
$('#entertype').click(function(){
    if (entertype==0) {
        clearInterval(time);
        entertype=1;
        $(this).text('切换到扫码模式');
        $(this).removeClass('btn-primary').addClass('btn-warning')
    }else{
        entertype=0;
        $(this).text('切换到手动模式');
        $(this).removeClass('btn-warning').addClass('btn-primary')
        time = setInterval(function(){
            // $('#scancode').focus();
            scancodefocus();
        },1000);
    }
})
$(document).click(function(){
    if (entertype==0) {
        // $('#scancode').focus();
        scancodefocus();
    }
})

function scancodefocus(){
    if ($(window.frameElement).css('display')=='inline') {
        $('#scancode').focus();
    }
}
</script>